package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentFacilityMapper apartmentFacilityMapper;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentLabelMapper apartmentLabelMapper;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private ApartmentFeeValueMapper apartmentFeeValueMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        // 1. 判断操作类型是新增还是修改，根据有无 id 来判断
        boolean isUpdate = apartmentSubmitVo.getId() != null;
        // 2. 保存基本信息
        super.saveOrUpdate(apartmentSubmitVo);
        // 3. 如果是更新操作，先删后增
        if (isUpdate) {
            // 3.1 删除图片列表
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT)
                    .eq(GraphInfo::getId, apartmentSubmitVo.getId());
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
            // 3.2 删除配套列表
            LambdaQueryWrapper<ApartmentFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            facilityLambdaQueryWrapper.eq(ApartmentFacility::getId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(facilityLambdaQueryWrapper);
            // 3.3 删除标签列表
            LambdaQueryWrapper<ApartmentLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            labelLambdaQueryWrapper.eq(ApartmentLabel::getId, apartmentSubmitVo.getId());
            apartmentLabelService.remove(labelLambdaQueryWrapper);
            // 3.4 删除杂费列表
            LambdaQueryWrapper<ApartmentFeeValue> feeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            feeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getId, apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(feeValueLambdaQueryWrapper);
        }
        // 4. 插入其他数据
        // 4.1 删除图片列表
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
//        List<GraphInfo> graphInfoList = graphInfoService.list();
//        for (GraphVo graphVo : graphVoList) {
//            GraphInfo.builder()
//                    .itemType(ItemType.APARTMENT)
//                    .itemId(apartmentSubmitVo.getId())
//                    .name(graphVo.getName())
//                    .url(graphVo.getUrl())
//                    .build();
//        }
        if (!CollectionUtils.isEmpty(graphVoList)) {
            List<GraphInfo> graphInfoList = graphVoList.stream()
                    .map(graphVo -> GraphInfo.builder()
                            .itemType(ItemType.APARTMENT)
                            .itemId(apartmentSubmitVo.getId())
                            .name(graphVo.getName())
                            .url(graphVo.getUrl())
                            .build()
                    ).collect(Collectors.toList());
            graphInfoService.saveBatch(graphInfoList);
        }

        // 4.2 删除配套列表
        List<Long> facilityInfoIdList = apartmentSubmitVo.getFacilityInfoIds();
//        List<ApartmentFacility> facilityList = new ArrayList<>();
//        for (Long facilityInfoId : facilityInfoIdList) {
//            ApartmentFacility apartmentFacility = new ApartmentFacility();
//            apartmentFacility.setId(facilityInfoId);
//            apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
//            facilityList.add(apartmentFacility);
//        }
        if (!CollectionUtils.isEmpty(facilityInfoIdList)) {
            List<ApartmentFacility> facilityList = facilityInfoIdList.stream()
                    .map(facilityInfoId -> ApartmentFacility.builder()
                            .facilityId(facilityInfoId)
                            .apartmentId(apartmentSubmitVo.getId())
                            .build()
                    ).collect(Collectors.toList());
            apartmentFacilityService.saveBatch(facilityList);
        }

        // 4.3 删除标签列表
        List<Long> labelIdList = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIdList)) {
            List<ApartmentLabel> labelList = labelIdList.stream()
                    .map(labelId -> ApartmentLabel.builder()
                            .labelId(labelId)
                            .apartmentId(apartmentSubmitVo.getId())
                            .build())
                    .collect(Collectors.toList());
            apartmentLabelService.saveBatch(labelList);
        }
        // 4.4 删除杂费列表
        List<Long> feeValueIdList = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIdList)) {
            List<ApartmentFeeValue> feeValueList = feeValueIdList.stream()
                    .map(labelId -> ApartmentFeeValue.builder()
                            .feeValueId(labelId)
                            .apartmentId(apartmentSubmitVo.getId())
                            .build())
                    .collect(Collectors.toList());
            apartmentFeeValueService.saveBatch(feeValueList);
        }

    }

    @Override
    public IPage<ApartmentItemVo> pageItem(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageItem(page, queryVo);
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        // 查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        // 查询图片列表
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, id);
        // 查询标签列表
        List<LabelInfo> labelList = apartmentLabelMapper.selectListByApartmentId(id);
        // 查询配套列表
        List<FacilityInfo> facilityInfoList = apartmentFacilityMapper.selectListByApartmentId(id);
        // 查询杂费列表
        List<FeeValueVo> feeValueVoList = apartmentFeeValueMapper.selectListByApartmentId(id);
        // 组装结果
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setLabelInfoList(labelList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return apartmentDetailVo;
    }

    @Override
    public void removeApartmentById(Long id) {
        // 判断公寓下是否有房间，有房间怎终止删除操作
        Long count = roomInfoMapper.selectCount(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId, id));
        if (count > 0) {
            throw new LeaseException(ResultCodeEnum.ADMIN_APARTMENT_DELETE_ERROR);
        }

        // 删除公寓信息
        super.removeById(id);
        // 删除图片列表
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT)
                .eq(GraphInfo::getId, id);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
        // 删除配套列表
        LambdaQueryWrapper<ApartmentFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        facilityLambdaQueryWrapper.eq(ApartmentFacility::getId, id);
        apartmentFacilityService.remove(facilityLambdaQueryWrapper);
        // 删除标签列表
        LambdaQueryWrapper<ApartmentLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        labelLambdaQueryWrapper.eq(ApartmentLabel::getId, id);
        apartmentLabelService.remove(labelLambdaQueryWrapper);
        // 删除杂费列表
        LambdaQueryWrapper<ApartmentFeeValue> feeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        feeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getId, id);
        apartmentFeeValueService.remove(feeValueLambdaQueryWrapper);
    }
}




